package com.web.base.auth.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.blue.jdbc.Page;
import com.web.base.auth.model.MenuAuth;
import com.web.base.auth.message.MenuAuthRequest;
import com.web.base.auth.message.MenuAuthResponse;

/**
 * 菜单权限转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月15日
 */
public class MenuAuthConvertor
{
	private MenuAuthConvertor()
	{
	}
	
	public static MenuAuth toMenuAuth(MenuAuthRequest request)
	{
		if (request == null)
			return null;
		
		MenuAuth menuAuth = new MenuAuth();
		menuAuth.setId(request.getId());
		menuAuth.setMenuId(request.getMenuId());
		menuAuth.setName(request.getName());
		menuAuth.setUrl(request.getUrl());
		menuAuth.setState(request.getState());
		return menuAuth;
	}
	
	public static MenuAuthResponse toResponse(MenuAuth menuAuth)
	{
		if (menuAuth == null)
			return null;
		
		MenuAuthResponse response = new MenuAuthResponse();
		response.setId(menuAuth.getId());
		response.setMenuId(menuAuth.getMenuId());
		response.setName(menuAuth.getName());
		response.setUrl(menuAuth.getUrl());
		response.setState(menuAuth.getState());
		response.setSelected(menuAuth.getSelected());
		return response;
	}
	
	public static List<MenuAuthResponse> toResponse(List<MenuAuth> list)
	{
		List<MenuAuthResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (MenuAuth menuAuth : list)
		{
			responseList.add(toResponse(menuAuth));
		}
		return responseList;
	}
	
	public static Map<Integer, MenuAuthResponse> toResponse(Map<Integer, MenuAuth> map)
	{
		Map<Integer, MenuAuthResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, MenuAuth> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue()));
		}
		return responseMap;
	}
	
	public static Page<MenuAuthResponse> toResponse(Page<MenuAuth> page)
	{
		if (page == null)
			return null;
		
		Page<MenuAuthResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList()));
		return responsePage;
	}
	
}
